import numpy as np
import pandas as pd
import sklearn as sk
from ItemCF import *
from Evaluation import *

import csv
from sklearn.metrics.pairwise import cosine_similarity
def main():
    pd.set_option('display.max_columns', None)
    orders = pd.read_csv("./tm_online_data/orders.csv", encoding='gbk')
    orderitem = pd.read_csv("./tm_online_data/Items_orders.csv", encoding='gbk')
    attritem = pd.read_csv("./tm_online_data/Items_attribute.csv", encoding="gbk")
    print('查看订单')
    print(orders.info())

    print('查看orderitem')
    print(orderitem.info())
    print('查看订单个数')
    print(attritem.info())
    print('orders订单重复')

    print(len(np.unique(orders['订单编号'].values)))
    print(len(np.unique(orderitem['订单编号'].values)))
    print(len(np.unique(orderitem['标题'].values)))
    print('说明订单虽然有重复，但种类一样，标题大量重复')
    print(len(attritem['宝贝ID'].values))

    print(len(np.unique(attritem['宝贝ID'].values)))
    print('说明宝贝id也有重复')
    print('查看重复但宝贝id')
    getNotone(attritem,['宝贝ID'])
    print('删除重复但宝贝id')
    attritem.drop_duplicates(subset=['宝贝ID'], keep='first', inplace=True)
    print(len(attritem['宝贝ID'].values))
    print('查看重复的orders')
    getNotone(orders,['订单编号'])
    print('这里先按照正常的来 排除 重复订单')
    orders.drop_duplicates(subset=['订单编号'], keep='first', inplace=True)
    print(len(orders['订单编号'].values))

    print("确定连接")

    print("orderAndItem连接")
    # orderAndItem = pd.merge(orders,orderitem,left_on=['订单编号'],right_on=['订单编号'])
    orderAndItem = pd.merge(orders, orderitem, on="订单编号")
    print("检测orderAndItem")
    print(orderAndItem.info())
    print(orderAndItem.shape)
    print("attritemAndItem连接")
    attritemAndItem = pd.merge(orderAndItem,attritem, on="标题",how='right')
    print(attritemAndItem.info())
    print("检测attritemAndItem")
    print(attritemAndItem.shape)

    print("构建关系矩阵")
    res= attritemAndItem.loc[:,['买家会员名','宝贝ID']]
    res["次数"] = 0

    print(res.shape)
    print(res.head())
    fe = res.groupby(['买家会员名','宝贝ID']).count().reset_index()

    print(fe.shape)
    # print(fe.head())
    print("构建透视表")
    fe = fe.pivot(index = '买家会员名',columns = '宝贝ID',values = '次数')
    # print(fe)
    fevalues = fe.fillna(0).values
    # print(fevalues)

    print("计算相似度矩阵")
    print("用户相似度矩阵")
    users = cosine_similarity(fevalues)
    print(users.shape)
    print("物品相似度矩阵")
    goods = cosine_similarity(fevalues.T)
    # print(goods)
    print(goods.shape)
    print(pd.DataFrame(goods).head())
    # fevalues = pd.DataFrame(goods)
    print("fevalues")



    # print(res.loc[:,'买家会员名'].head())
    #
    # print(data['num_user'])
    item = ItemCF()
    print(fevalues.shape)
    print('物品相似度')
    goodsitem =  item.predict(fevalues,goods,'item')
    print(goodsitem.shape)
    print('用户相似度')
    userssitem =  item.predict(fevalues,users,'user')
    print(userssitem.shape)
    print('确定最终推荐函数')
    print('物品')
    goodsget = item.get_recom(fe,goodsitem,5)
    print(goodsget)

    print('用户')
    usersget = item.get_recom(fe,userssitem,5)
    print(usersget)
    eva = Evaluation( usersget,goodsget)

    eva.evaluate()







def getNotone(test_df,groupby):
    # 显示所有列
    # pd.set_option('display.max_columns', None)
    # 显示所有行
    # pd.set_option('display.max_rows', None)
    # 设置value的显示长度为100，默认为50
    pd.set_option('max_colwidth', 100)
    pd.set_option('display.float_format', lambda x: '%.0f' % x)

    df1 = test_df.groupby(groupby).size()
    col = df1[df1 > 1].reset_index()[groupby]
    # print(col)

    pd.merge(col, test_df, on=groupby)
    # print(test_df)
    print(test_df[test_df[groupby[0]] ==list(col.loc[0,[groupby[0]]])[0]])
    # print(list(col.loc[0,[groupby[0]]])[0])

if __name__ == '__main__':
    main()